﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace COC.Commons
{
    public class GameData
    {
        private readonly XmlDocument _ConfigXml = new XmlDocument();

        public GameData()
        {
            _ConfigXml.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GameData.xml"));
        }

        /// <summary>
        /// 根据节点名获取节点的标题数组
        /// </summary>
        /// <param name="name">节点名</param>
        /// <returns></returns>
        public Dictionary<string, string> GetItemTitle(string name)
        {
            var title = new Dictionary<string, string>();



            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode($"/COC_GameData/{name}/leveldata/title");
            if (srcElement == null)
            {
                return title;
            }
            {
                foreach (XmlElement ele in srcElement.ChildNodes)
                {
                    var key = ele.Name;
                    var value = ele.GetAttribute("text");
                    title.Add(key, value);
                }
            }
            return title;
        }

        /// <summary>
        /// 根据节点名获取节点的各等级数据，每个等级的数据表现为一个字典，字典的key和字典的key相同的放入同一列。
        /// </summary>
        /// <param name="name">节点名</param>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetItemData(Dictionary<string, string> title, string name)
        {
            List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode($"/COC_GameData/{name}/leveldata");
            if (srcElement == null)
            {
                return data;
            }
            {
                
                foreach (XmlElement ele in srcElement.ChildNodes)
                {
                    if (!ele.Name.Equals("level"))
                        continue;
                    var level = new Dictionary<string, string>();
                    //用标题字典获取每个字典
                    foreach (var key in title.Keys)
                    {
                        var value = ele.GetAttribute(key);
                        level.Add(key, value);
                    }
                    data.Add(level);
                }
            }

            return data;
        }
    }
}
